from system.action import Action

class InputMap(Action):
    def dist(self, cmd, value, b):
        #self.log("dist",cmd,value,b,self.object.vars)
        if self.object.hasVar(cmd):
            #self.log("dist to", self.object.getVar(cmd,None))
            self.publish(self.object.getVar(cmd,None),value = value, b = b)
    
    def receive_keyDown(self, args):
        if args['name']:
            self.dist("key_%s" % args['name'], 1, True)
            for mod in args['mods']:
                self.dist("key_%s_%s" % (mod,args['name']), 1, True)
        
    def receive_keyUp(self, args):
        if args['name']:
            self.dist("key_%s" % args['name'], 0, False)
            for mod in args['mods']:
                self.dist("key_%s_%s" % (mod,args['name']), 0, False)
                
    def receive_mouseButtonDown(self, args):
        if args['buttonName']:
            self.dist("mouseButton_%s" % args['buttonName'], 1, True)
        self.dist("mouseButton_%d" % args['button'], 1, True)
        
    def receive_mouseButtonUp(self, args):
        if args['buttonName']:
            self.dist("mouseButton_%s" % args['buttonName'], 0, False)
        self.dist("mouseButton_%d" % args['button'], 0, False)
        
    def receive_joyButtonDown(self, args):
        self.dist("joyButton_%d_%d" % (args['joy'],args['button']), 1, True)

    def receive_joyButtonUp(self, args):
        self.dist("joyButton_%d_%d" % (args['joy'],args['button']), 0, False)
        
    def receive_joyAxisMotion(self, args):
        if args['value'] < 0:
            dir = 'neg'
        else:
            dir = 'pos'
        self.dist("joyAxis_%s_%d_%d" % (dir,args['joy'],args['axis']), abs(args['value']), abs(args['value']) > 0.5)
